<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org">
<head th:replace="common/fragment::top">

</head>

<body>
<div class="lyear-layout-web">
  <div class="lyear-layout-container">
    <!--左侧导航-->
    <div th:replace="common/fragment::leftmenu"></div>
    <!--End 左侧导航-->

    <!--头部信息-->
    <div th:replace="common/fragment::header"></div>
    <!--End 头部信息-->

    <!--页面主要内容-->
    <main class="lyear-layout-content">
      <div class="container-fluid">
        <div class="row show-grid col-md-12">
          <div class="col-md-4">
            <label class="col-md-4" for="sellchoice" style="margin: 5px 0">采购编号</label>
            <span class="form-control" id="sellchoice" th:text="${sellOut.sellPO.name}"></span>
          </div>
          <div class="col-md-4">
            <label class="col-md-4" for="cust" style="margin: 5px 0">顾客</label>
            <span class="form-control" id="cust"
                  th:text="${sellOut.sellPO.custPO.name}"></span>
          </div>
          <div class="col-md-4">
            <label class="col-md-4" style="margin: 5px 0">要货日期</label>
            <span class="form-control"
                  th:text="${#dates.format(sellOut.sellPO.need, 'yyyy-MM-dd')}"></span>
          </div>
        </div>
        <ul class="nav nav-list">
          <li class="divider"></li>
        </ul>
        <div class="row show-grid col-md-12">
          <div class="col-md-4">
            <input hidden="hidden" id="sellOutId" th:value="${sellOut.id}">
            <label class="col-md-4" style="margin: 5px 0">入库编号</label>
            <span class="form-control" th:text="${sellOut.name}"></span>
          </div>
          <div class="col-md-4">
            <label class="col-md-4" style="margin: 5px 0">编制日期</label>
            <span class="form-control"
                  th:text="${#dates.format(sellOut.sellPO.createTime, 'yyyy-MM-dd')}"></span>
          </div>

          <div class="col-md-4">
            <label class="col-md-4" style="margin: 5px 0">责任人</label>
            <span class="form-control" th:text="${sellOut.userPO.username}"></span>
          </div>
        </div>
      </div>


      <!--入库明细-->
      <div id="detail-table">
        <table class="table table-bordered">
          <thead id="goodheader">
          <tr>
            <!--                <th>序号</th>-->
            <th hidden="hidden">明细id</th>
            <th hidden="hidden">商品id</th>
            <th>商品名称</th>
            <th>商品型号</th>
            <!--    <th>角色图标</th>-->
            <th>商品规格</th>
            <!--                                            <th>包装</th>-->
            <th>入库数量</th>
            <th>当前库存</th>
            <th>更新</th>
            <!--    <th>操作</th>-->
          </tr>
          </thead>
          <tbody>
          <!--/*@thymesVar id="sellDetailVOList" type="com.examplemartin.jpademo.pojo.vo.sell.PurDetailVO"*/-->
          <tr class="detail" th:each="detail:${sellOut.sellOutDetailPOList}">
            <td class="detailid" hidden="hidden" th:text="${detail.id ne null?detail.id:'-999'}">
              明细id
            </th>
            <td class="goodid" hidden="hidden" th:text="${detail.goodPO.id}">
              商品id
            </td>
            <td th:text="${detail.goodPO.name}">商品名称</td>
            <td th:text="${detail.goodPO.spec}">商品型号</td>
            <!--    <th>角色图标</th>-->
            <td th:text="${detail.goodPO.unit}">商品规格</td>
            <td class="oldcount" th:text="${detail.goodcount}">入库数量</td>
            <td class="currCountInstock" th:text="${repomap[detail.goodPO.id]}">当前库存</td>
            <td><input class="form-control input-sm incount" placeholder="修订数量" type="number"></td>
          </tr>

          </tbody>
          <div class="col-md-2" id="save-detail-btn-div">
            <button
                class="btn-xs btn-info ajax-post col-md-12"
                id="save-detail-btn"
                type="button">保存入库单
            </button>
          </div>
        </table>

      </div>
      <!--END添加明细-->
    </main>
    <!--End 页面主要内容-->
  </div>
</div>

<div th:insert="common/fragment::tail-script"></div>

<style>
  .modify {
    color: #ac2925;
    font-weight: bolder;
  }

  .hiddendetail {
    display: none;
  }

  .changedetail {
    color: #ff0000;
    font-weight: bolder;
  }
</style>

<script type="text/javascript">
  $(function () {
    let save_flag = true

// 输入数量 为整数
    $('.incount').each(function (i, e) {
      $(e).blur(function () {
        let val = parseInt($(e).val())
        console.log(val)

        if (val == null || val === "Null" || val === '' || isNaN(val)) {
          $(this).val(null)
          return
        }

        if (val < 0) {
          confirmMsg("输入数量必须大于0")
          return false;
        }

        let oldval = $(this).parents('tr').find('.oldcount').text()
        oldval = parseInt(oldval)
        if (parseInt(val) >= 0 && parseInt(val) !== oldval) {
          $(e).parents('tr').addClass('changedetail')
        } else {
          $(e).parents('tr').removeClass('changedetail')
        }
        $(this).val(val)

      });
    });

// 保存订单
    $('#save-detail-btn').click(function () {
      let sellOutId = $('#sellOutId').val()
      // 明细

      // 循环有变化的数值
      let sellOutDetailPOList = []
      $('.changedetail').each(function (i, e) {
        let detailid = $(this).children('.detailid').text()
        let element = null
        if (detailid === '-999') {
          let goodcount = $(this).find('.incount').val()
          goodcount = parseInt(goodcount)
          let goodid = $(this).children('.goodid').text()
          goodid = parseInt(goodid)
          element = {
            goodPO: {id: goodid},
            goodcount: goodcount
          }
        } else {
          let detailid = $(this).children('.detailid').text()
          detailid = parseInt(detailid)
          let oldval = $(this).children('.oldcount').text()
          oldval = parseInt(oldval)
          let newval = $(this).find('.incount').val()
          newval = parseInt(newval)
          element = {
            id: detailid,
            goodcount: newval
          }
        }
        sellOutDetailPOList.push(element)

        // 判断是否已超出销售订单数量
        $('#'+detailtable).children('tr').each(function (i,e) {
          let goodcount = $(e).children('.goodcount').text()
          let outStockCount = $(e).children('.outStockCount').text()
          let incount = $(e).find('.incount').val()
          let currCountInstock = $(e).find('.currCountInstock').text()

          if(parseInt(currCountInstock)<parseInt(incount)){
            save_flag=false
            confirmMsg("现有库存不足")
          }

          if(parseInt(goodcount)<(parseInt(outStockCount)+parseInt(incount)))
          {
            save_msg = "出库数量超出订单"
            confirmMsg(save_msg)
            save_flag=false
            return false
          }else{
            // 循环有变化的数值
            let sellOutDetailPOList = []
            $('#'+detailtable).children('tr').each(function (i,e) {
              // console.log($(e).children('.goodid').text())
              let val = $(e).find('.incount').val()

              if(val>0){
                let val = parseInt($(e).find('.incount').val())
                let goodid = $(e).children('.goodid').text()

                let goodPO = {id:goodid}
                let SellOutDetailPO = {
                  goodPO:goodPO,
                  goodcount:val
                }
                sellOutDetailPOList.push(SellOutDetailPO)
              }
            });
            // 生成json
            data = {
              sellPO:sellPO,
              sellOutDetailPOList:sellOutDetailPOList
            }
            // // 发送ajax
            // post_ajax("/sellout/savesellout.ajax",data,"/sellout/selloutstock.do")
          }
          if(save_flag==false){
            return false;
          }

        })

      });
      // 生成json
      let data = {
        id: sellOutId,
        sellOutDetailPOList: sellOutDetailPOList
      }

      console.log(data)
      // 发送ajax
      if(save_flag){
        post_ajax("/sellout/editsellout.ajax", data, "/sellout/selloutstock.do")
      }else {
        confirmMsg("请检查输入")
      }

    });

  });
</script>
</body>
</html>